package com.cloudptt.api.product.func.media.device;

import android.content.Intent;
import android.content.IntentFilter;
import android.view.KeyEvent;

import com.cloudptt.api.comm.Const;
import com.cloudptt.api.product.func.media.EventListener;
import com.cloudptt.api.product.log.Log;

public class PNC370 implements IDeviceKey {
	public final static String THIS_FILE = "PNC370";
	private static final String BRAND = "qcom";
	private static final String DEVICE = "msm8909";
	private static final String MODEL = "PNC 370";
	private static final String MODEL_SE = "PNC370SE";
	private static final String MODEL_360SE = "PNC360S";
//	private boolean isRing = false;
	private long pttOnTimer =  0;

	private IntentFilter mFilter;
	 private final static String ACTION_BRAST = "com.meigsmart.meigkeyaccessibility.onkeyevent";

	public PNC370(){
		
	}
	
	public static boolean isThisDev(String brand,String device,String model){
		if(brand == null
				|| device == null
				|| model == null){
			return false;
		}
		else if(BRAND.equals(brand)
				&& DEVICE.equals(device)
				&& MODEL.equals(model)){
			return true;
		}
		else if(BRAND.equals(brand)
				&& DEVICE.equals(device)
				&& MODEL_SE.equals(model)){
			return true;
		}
		else if(BRAND.equals(brand)
				&& DEVICE.equals(device)
				&& MODEL_360SE.equals(model)){
			return true;
		}
		return false;
	}

	@Override
	public IntentFilter getBstIntentFilter() {
		mFilter = new IntentFilter();
		/**蓝牙PTT**/
		mFilter.addAction(ACTION_BRAST);/**蓝牙指环PTT**/
		
		return mFilter;
	}
	
	@Override
	public void procBst(Intent intent, EventListener listener) {
		String action = intent.getAction();
		Log.d(THIS_FILE, " procBst action = " + action);
		if(action.equals(ACTION_BRAST)){
			int actionKey = intent.getIntExtra("action", -1);
			int keycode = intent.getIntExtra("keycode", -1);
//这里可以对按键做你想要的处理
			if(KeyEvent.KEYCODE_F12 == keycode) {
				if (actionKey == KeyEvent.ACTION_DOWN) {
					if (listener != null) {
						listener.onPtt(Const.MSG_TALK_ON, false, true);
					}
				} else if (actionKey == KeyEvent.ACTION_UP) {
					if (listener != null) {
						listener.onPtt(Const.MSG_TALK_OFF, false, true);
					}
				}
			}
			else if(KeyEvent.KEYCODE_F2 == keycode) {
				if (actionKey == KeyEvent.ACTION_DOWN) {
					pttOnTimer = System.currentTimeMillis();
				}
				else if (actionKey == KeyEvent.ACTION_UP) {
					long val = System.currentTimeMillis();
					boolean isLongPress = (val - pttOnTimer) > 2000;
					if (listener != null) {
						listener.onSOS(isLongPress);
					}
				}
            }
		}
	}
}
